#Word order experiment - Phase 3

#Modules to import

import datetime
import time
import random
from random import shuffle
import xlwt
import xlrd
import xlutils
from xlutils import copy
from xlrd import open_workbook
import pandas as pd
import numpy as np

#PsychoPy
import psychopy
from psychopy import visual, core, event, gui
from psychopy.visual import Window, TextStim
from psychopy.core import wait
from psychopy.event import waitKeys


#--------------------------------------------------------------------------------------

#****************************Excel file for data collection - UPDATE THIS!***********************************************

filename = 'scoring3_user1.xls'


#Functions

def timeNow(): #get current time
    now = datetime.datetime.now()
    return now

def getWindow(): #get window for experiment
    window = psychopy.visual.Window([1200,600], allowGUI=True, color=[0,0,1], units='pix', fullscr=True) #add fullscr=True
    return window

def InstructionsStart(window, display_duration, now, sheet1): #annotate user sheet + display greeting on screen
    sheet1.write(1,6, now.strftime('%H:%M'))
    sheet1.write(1,8, now.strftime('%d-%m-%Y'))
    greet = psychopy.visual.TextStim(window, text = "Welcome to the experiment!", height = 30, wrapWidth = 50000)
    greet.draw() #draw the greeting
    window.flip()
    psychopy.core.wait(display_duration)
    return sheet1

def getStimuli(lines):
    stimuli = []
    for line in lines:
        line = line.strip()
        stimuli.append(line)
    setsize = len(stimuli)
    return stimuli, setsize

def getCompleteWords(words):
    completeWords = []
    for word in words:
        word = word.strip()
        completeWords.append(word)
    return completeWords

def displayDuration(): #get display duration/inter-stimulus interval (ISI) in seconds
    display_duration = 4500 #ms.
    ISI = 2500 #inter-stimulus interval in ms.
    display_duration = display_duration / 1000.0 #sec.
    ISI = ISI / 1000.0
    return display_duration, ISI


#ADD GENERATION ASPECT

def learningPhase(window, ISI, stimuli, setsize, display_duration, completeWords): #LEARNING PHASE
    begin = psychopy.visual.TextStim(window, text = "Learn the words that appear on the screen.", pos=(0.0,0.0), height = 30, wrapWidth = 50000)
    fillin = psychopy.visual.TextStim(window, text = "Type the missing letter for each word.", pos=(0.0,-50), height = 30, wrapWidth = 50000)
    begin.draw()
    fillin.draw()                
    window.flip()
    psychopy.core.wait(display_duration)
    for i in range(setsize):
        text_stimulus = psychopy.visual.TextStim(window, text = stimuli[i], height = 30) #height = font of the words
        text_stimulus.draw()
        window.flip()
        answer = psychopy.event.waitKeys(keyList =['a','e','i','o','u'])
        text_stimulus.draw()
        complete_word = psychopy.visual.TextStim(window, text = completeWords[i], height = 30, pos = (0.0,-100))
        complete_word.draw()
        window.flip()
        psychopy.core.wait(ISI)

    probe_instructions = psychopy.visual.TextStim(window, text = 'You will now be tested on the words you just learned.', wrapWidth = 50000, height = 30)
    probe_instructions.draw()
    window.flip()
    psychopy.core.wait(display_duration)
    return stimuli


def listReview(window, ISI, stimuli, setsize, display_duration, completeWords):
    begin = psychopy.visual.TextStim(window, text = "Review the words that appear on the screen.", pos=(0.0,0.0), height = 30, wrapWidth = 50000)
    fillin = psychopy.visual.TextStim(window, text = "Type the missing letter for each word.", pos=(0.0,-50), height = 30, wrapWidth = 50000)
    begin.draw()
    fillin.draw()
    window.flip()
    psychopy.core.wait(display_duration)
    for i in range(setsize):
        text_stimulus = psychopy.visual.TextStim(window, text = stimuli[i], height = 30) #height = font of the words
        text_stimulus.draw()
        window.flip()
        answer = psychopy.event.waitKeys(keyList =['a','e','i','o','u'])
        text_stimulus.draw()
        complete_word = psychopy.visual.TextStim(window, text = completeWords[i], height = 30, pos = (0.0,-100))
        complete_word.draw()
        window.flip()
        psychopy.core.wait(ISI)
        
    probe_instructions = psychopy.visual.TextStim(window, text = 'You will now be tested again on the words you just reviewed.', wrapWidth = 50000, height = 30)
    probe_instructions.draw()
    window.flip()
    psychopy.core.wait(display_duration)


def probeTrial(window, ISI, display_duration, probeAR): #TESTING PHASE

    probe_message = psychopy.visual.TextStim(window, text = 'In which order were these words presented?', pos = (0, 20), height = 30, wrapWidth = 50000)
    counter = 0
    
    probeARS = probeAR[:]
    shuffle(probeARS)

    #correct answer is choice 1

    correct = -1

    for i in range(len(probeARS)):
        if probeARS[i] == probeAR[0]:
            correct = i + 1
            
    correct = str(correct)
    new_corr = []
    new_corr.append(correct)


    option1 = psychopy.visual.TextStim(window, text='(1) ' + probeARS[0], pos = (0, -25), height = 30)
    option2 = psychopy.visual.TextStim(window, text='(2) ' + probeARS[1], pos = (0, -55), height = 30)    
    option3 = psychopy.visual.TextStim(window, text='(3) ' + probeARS[2], pos = (0, -85), height = 30)
    option4 = psychopy.visual.TextStim(window, text='(4) ' + probeARS[3], pos = (0, -115), height = 30)
    option5 = psychopy.visual.TextStim(window, text='(5) ' + probeARS[4], pos = (0, -145), height = 30)
    option6 = psychopy.visual.TextStim(window, text='(6) ' + probeARS[5], pos = (0, -175), height = 30)


    probe_message.draw()
    option1.draw()
    option2.draw()
    option3.draw()
    option4.draw()
    option5.draw()
    option6.draw()

    window.flip()

    answer = psychopy.event.waitKeys(keyList = ['1','2','3','4','5','6'])

 
    if answer == new_corr:
        corr = psychopy.visual.TextStim(window, text='You are correct!', pos = (0,0), height = 30)
        corr.draw()
        window.flip()
        psychopy.core.wait(display_duration)
        counter = counter + 1 #counter = 1
                                #correct on 1st attempt
        return counter
        
            
    else:
        incorrect = psychopy.visual.TextStim(window, text='Wrong choice. Try again', pos = (0,0), height = 30, wrapWidth=500000) #begin attempt 2
        incorrect.draw()
        window.flip()
        psychopy.core.wait(display_duration)
        x = int(answer[0]) - 1
        del probeARS[x]
        second_probeARS = probeARS[:]
        shuffle(second_probeARS) #new shuffled probe list
        correct2 = -1

        for i in range(len(second_probeARS)):
            if second_probeARS[i] == probeAR[0]:
                correct2 = i + 1
            
        correct2 = str(correct2)
        new_corr2 = []
        new_corr2.append(correct2)

        
        option1_2 = psychopy.visual.TextStim(window, text='(1) ' + second_probeARS[0], pos = (0, -25), height = 30)
        option2_2 = psychopy.visual.TextStim(window, text='(2) ' + second_probeARS[1], pos = (0, -55), height = 30)    
        option3_2 = psychopy.visual.TextStim(window, text='(3) ' + second_probeARS[2], pos = (0, -85), height = 30)
        option4_2 = psychopy.visual.TextStim(window, text='(4) ' + second_probeARS[3], pos = (0, -115), height = 30)
        option5_2 = psychopy.visual.TextStim(window, text='(5) ' + second_probeARS[4], pos = (0, -145), height = 30)

        option1_2.draw()
        option2_2.draw()
        option3_2.draw()
        option4_2.draw()
        option5_2.draw()


        window.flip()
        
        
        answer2 = psychopy.event.waitKeys(keyList = ['1','2','3','4','5'])



        if answer2 == new_corr2:
            corr = psychopy.visual.TextStim(window, text='You are correct!', pos = (0,0), height = 30)
            corr.draw()
            window.flip()
            psychopy.core.wait(display_duration)
            counter = counter + 2 #counter = 2
                                #correct on 2nd attempt
            return counter
            
        else:
            incorrect = psychopy.visual.TextStim(window, text='Wrong choice. Try again', pos = (0,0), height = 30, wrapWidth=500000) #begin attempt 3
            incorrect.draw()
            window.flip()
            psychopy.core.wait(display_duration)
            y = int(answer2[0]) - 1
            del second_probeARS[y]
            third_probeARS = second_probeARS[:]
            shuffle(third_probeARS) #new shuffled probe list
            correct3 = -1

            for i in range(len(third_probeARS)):
                if third_probeARS[i] == probeAR[0]:
                    correct3 = i + 1
            
            correct3 = str(correct3)
            new_corr3 = []
            new_corr3.append(correct3)

        
            option1_3 = psychopy.visual.TextStim(window, text='(1) ' + third_probeARS[0], pos = (0, -25), height = 30)
            option2_3 = psychopy.visual.TextStim(window, text='(2) ' + third_probeARS[1], pos = (0, -55), height = 30)    
            option3_3 = psychopy.visual.TextStim(window, text='(3) ' + third_probeARS[2], pos = (0, -85), height = 30)
            option4_3 = psychopy.visual.TextStim(window, text='(4) ' + third_probeARS[3], pos = (0, -115), height = 30)

            option1_3.draw()
            option2_3.draw()
            option3_3.draw()
            option4_3.draw()


            window.flip()
        
        
            answer3 = psychopy.event.waitKeys(keyList = ['1','2','3','4'])


            if answer3 == new_corr3:
                corr = psychopy.visual.TextStim(window, text='You are correct!', pos = (0,0), height = 30)
                corr.draw()
                window.flip()
                psychopy.core.wait(display_duration)
                counter = counter + 3 #counter = 3
                                    #correct on 3rd attempt
                return counter
            
            else:
                incorrect = psychopy.visual.TextStim(window, text='Wrong choice', pos = (0,0), height = 30, wrapWidth=500000) #begin attempt 4
                incorrect.draw()
                window.flip()
                psychopy.core.wait(display_duration)
                counter = counter + 4 #counter = 4
                                    #all incorrect
                return counter
        

#FOIL FUNCTION
def foilQuestion(window, ISI, display_duration, probeFO):
    foil_message = psychopy.visual.TextStim(window, text = 'Which of the following words was presented in the list?', pos = (0, 20), height = 30, wrapWidth = 50000)
    counter = 0
    
    probeFOS = probeFO[:]
    shuffle(probeFOS)

    #correct answer is choice 1

    correct = -1

    for i in range(len(probeFOS)):
        if probeFOS[i] == probeFO[0]:
            correct = i + 1
            
    correct = str(correct)
    new_corr = []
    new_corr.append(correct)


    option1 = psychopy.visual.TextStim(window, text='(1) ' + probeFOS[0], pos = (-100, -25), alignHoriz = 'left', height = 30)
    option2 = psychopy.visual.TextStim(window, text='(2) ' + probeFOS[1], pos = (-100, -55), alignHoriz = 'left', height = 30)    
    option3 = psychopy.visual.TextStim(window, text='(3) ' + probeFOS[2], pos = (-100, -85), alignHoriz = 'left', height = 30)
    option4 = psychopy.visual.TextStim(window, text='(4) ' + probeFOS[3], pos = (-100, -115), alignHoriz = 'left', height = 30)


    foil_message.draw()
    option1.draw()
    option2.draw()
    option3.draw()
    option4.draw()

    window.flip()

    answer = psychopy.event.waitKeys(keyList = ['1','2','3','4'])

 
    if answer == new_corr:
        corr = psychopy.visual.TextStim(window, text='You are correct!', pos = (0,0), height = 30)
        corr.draw()
        window.flip()
        psychopy.core.wait(display_duration)
        counter = counter + 1 #counter = 1
                                #correct on 1st attempt
        return counter
        
            
    else:
        incorrect = psychopy.visual.TextStim(window, text='Wrong choice. Try again', pos = (0,0), height = 30, wrapWidth=500000) #begin attempt 2
        incorrect.draw()
        window.flip()
        psychopy.core.wait(display_duration)
        x = int(answer[0]) - 1
        del probeFOS[x]
        second_probeFOS = probeFOS[:]
        shuffle(second_probeFOS) #new shuffled probe list
        correct2 = -1

        for i in range(len(second_probeFOS)):
            if second_probeFOS[i] == probeFO[0]:
                correct2 = i + 1
            
        correct2 = str(correct2)
        new_corr2 = []
        new_corr2.append(correct2)

        
        option1_2 = psychopy.visual.TextStim(window, text='(1) ' + second_probeFOS[0], pos = (-100, -25), alignHoriz = 'left', height = 30)
        option2_2 = psychopy.visual.TextStim(window, text='(2) ' + second_probeFOS[1], pos = (-100, -55), alignHoriz = 'left', height = 30)    
        option3_2 = psychopy.visual.TextStim(window, text='(3) ' + second_probeFOS[2], pos = (-100, -85), alignHoriz = 'left', height = 30)

        option1_2.draw()
        option2_2.draw()
        option3_2.draw()


        window.flip()
        
        
        answer2 = psychopy.event.waitKeys(keyList = ['1','2','3'])



        if answer2 == new_corr2:
            corr = psychopy.visual.TextStim(window, text='You are correct!', pos = (0,0), height = 30)
            corr.draw()
            window.flip()
            psychopy.core.wait(display_duration)
            counter = counter + 2 #counter = 2
                                #correct on 2nd attempt
            return counter
            
        else:
            incorrect = psychopy.visual.TextStim(window, text='Wrong choice. Try again', pos = (0,0), height = 30, wrapWidth=500000) #begin attempt 3
            incorrect.draw()
            window.flip()
            psychopy.core.wait(display_duration)
            y = int(answer2[0]) - 1
            del second_probeFOS[y]
            third_probeFOS = second_probeFOS[:]
            shuffle(third_probeFOS) #new shuffled foil list
            correct3 = -1

            for i in range(len(third_probeFOS)):
                if third_probeFOS[i] == probeFO[0]:
                    correct3 = i + 1
            
            correct3 = str(correct3)
            new_corr3 = []
            new_corr3.append(correct3)

        
            option1_3 = psychopy.visual.TextStim(window, text='(1) ' + third_probeFOS[0], pos = (-100, -25), alignHoriz = 'left', height = 30)
            option2_3 = psychopy.visual.TextStim(window, text='(2) ' + third_probeFOS[1], pos = (-100, -55), alignHoriz = 'left', height = 30)    
  
            option1_3.draw()
            option2_3.draw()


            window.flip()
        
        
            answer3 = psychopy.event.waitKeys(keyList = ['1','2'])


            if answer3 == new_corr3:
                corr = psychopy.visual.TextStim(window, text='You are correct!', pos = (0,0), height = 30)
                corr.draw()
                window.flip()
                psychopy.core.wait(display_duration)
                counter = counter + 3 #counter = 3
                                    #correct on 3rd attempt
                return counter
            
            else:
                incorrect = psychopy.visual.TextStim(window, text='Wrong choice', pos = (0,0), height = 30, wrapWidth=500000) #begin attempt 4
                incorrect.draw()
                window.flip()
                psychopy.core.wait(display_duration)
                counter = counter + 4 #counter = 4
                                    #all incorrect
                return counter



def nextQuestion(window, display_duration): #screen b/w questions
    nextQ = psychopy.visual.TextStim(window, text = 'Next Question!', pos = (0,0), height = 30)
    nextQ.draw()
    window.flip()
    psychopy.core.wait(display_duration)
    
def test1Mess(window, display_duration): #signal first test
    test1 = psychopy.visual.TextStim(window, text = 'Test 1', pos = (0,0), height = 30)
    test1.draw()
    window.flip()
    psychopy.core.wait(display_duration)

def test2Mess(window, display_duration): #signal second test
    test1 = psychopy.visual.TextStim(window, text = 'Test 2', pos = (0,0), height = 30)
    test1.draw()
    window.flip()
    psychopy.core.wait(display_duration)

def test3Mess(window, display_duration): #signal third test
    test1 = psychopy.visual.TextStim(window, text = 'Test 3', pos = (0,0), height = 30)
    test1.draw()
    window.flip()
    psychopy.core.wait(display_duration)

def test4Mess(window, display_duration): #signal fourth test
    test1 = psychopy.visual.TextStim(window, text = 'Test 4', pos = (0,0), height = 30)
    test1.draw()
    window.flip()
    psychopy.core.wait(display_duration)

def getScoringSheet(): #write column names on user sheet
    book = xlwt.Workbook()
    sheet1 = book.add_sheet('Sheet 1')
    sheet1.write(0,0, 'Trial #')
    sheet1.write(0,1, '1st Attempt')
    sheet1.write(0,2, '2nd Attempt')
    sheet1.write(0,3, '3rd Attempt')
    sheet1.write(0,4, 'All Incorrect')
    sheet1.write(0,5, 'Probe Type')
    sheet1.write(0,6, 'Start Time')
    sheet1.write(0,7, 'End Time')
    sheet1.write(0,8, 'Date')
    sheet1.write(0,9, 'List #')
    
    for i in range(1,49): #48 trials
        sheet1.write(i,0, i)
        
    book.save(filename)
    
    return sheet1, book
    

def Scoring(counter, cell, start, sheet1, book): #update scores on user sheet
    
    if counter == 1:
        start = start + 1
        sheet1.write(cell, 1, start)
    elif counter == 2:
        start = start + 1
        sheet1.write(cell, 2, start)
    elif counter == 3:
        start = start + 1
        sheet1.write(cell, 3, start)
    else:
        start = start + 1
        sheet1.write(cell, 4, start)

    book.save(filename)
        
    return start, sheet1

def probeType(book, sheet1, cell, probe_i, probeNU, probeFU): #update user sheet with probe type (AR/AU...)
    if probe_i == probeNU:
        sheet1.write(cell, 5, 'NU')
    elif probe_i == probeFU:
        sheet1.write(cell, 5, 'FU')
    #elif probe_i == probeFO:
     #   sheet1.write(cell, 5, 'FO')
        
    book.save(filename)

    return sheet1


def foilType(book, sheet1, cell, probeFO):
    sheet1.write(cell,5,"FO")
    book.save(filename)
    
    return sheet1

def listNum(lin, lines, sheet1, cell, book): #update user sheet with list number (1-4)
    if lin == lines[0]:
        sheet1.write(cell, 9, '1')
    elif lin == lines[1]:
        sheet1.write(cell, 9, '2')
    elif lin == lines[2]:
        sheet1.write(cell, 9, '3')
    elif lin == lines[3]:
        sheet1.write(cell, 9, '4')

    book.save(filename)

    return sheet1
    

def Goodbye(window, display_duration, now2, sheet1, book): #update user sheet + display goodbye message on screen
    sheet1.write(1,7, now2.strftime('%H:%M'))
    goodbye = psychopy.visual.TextStim(window, text = 'You have reached the end of the experiment!', pos = (0,0), wrapWidth=50000, height = 30)
    thankyou = psychopy.visual.TextStim(window, text = 'Thank you for participating!', pos = (0,-40), wrapWidth=50000, height = 30)
    goodbye.draw()
    thankyou.draw()
    window.flip()
    psychopy.core.wait(display_duration)
    return sheet1
    
#-------------------------------------------------------------------------------------------------------------------------------------

#Main

def main():
    cell = 0
    now = timeNow()
    sheet1, book = getScoringSheet()
    window = getWindow()
    display_duration, ISI = displayDuration()
    sheet1 = InstructionsStart(window, ISI, now, sheet1)
    
    lines1 = readWordList1()
    lines2 = readWordList2()
    lines3 = readWordList3()
    lines4 = readWordList4()


    lines, lines_shuffled = listRandomizer(lines1, lines2, lines3, lines4) #get list of word lists & randomized list of word lists


    for lin in lines_shuffled:
        stimuli, setsize = getStimuli(lin)
        #NU, FU = assignCategory(stimuli)
        if lin == lines[0]: #probes & foils from word list 1 + complete words
            probeNU = getProbeNU_1_1()
            probeNU2 = getProbeNU_1_2()
            probeNU3 = getProbeNU_1_3()
            probeNU4 = getProbeNU_1_4()
            probeFU = getProbeFU_1_1()
            probeFU2 = getProbeFU_1_2()
            probeFU3 = getProbeFU_1_3()
            probeFU4 = getProbeFU_1_4()
            probeFO = getFoilFO_1_1()
            probeFO2 = getFoilFO_1_2()
            probeFO3 = getFoilFO_1_3()
            probeFO4 = getFoilFO_1_4()
            completeWords = readCompleteWords1()

            #letters = correctLetters1()
            
        elif lin == lines[1]: #probes & foils from word list 2
            probeNU = getProbeNU_2_1()
            probeNU2 = getProbeNU_2_2()
            probeNU3 = getProbeNU_2_3()
            probeNU4 = getProbeNU_2_4()
            probeFU = getProbeFU_2_1()
            probeFU2 = getProbeFU_2_2()
            probeFU3 = getProbeFU_2_3()
            probeFU4 = getProbeFU_2_4()
            probeFO = getFoilFO_2_1()
            probeFO2 = getFoilFO_2_2()
            probeFO3 = getFoilFO_2_3()
            probeFO4 = getFoilFO_2_4()
            completeWords = readCompleteWords2()

            #letters = correctLetters2()
            
        elif lin == lines[2]: #probes & foils from word list 3
            probeNU = getProbeNU_3_1()
            probeNU2 = getProbeNU_3_2()
            probeNU3 = getProbeNU_3_3()
            probeNU4 = getProbeNU_3_4()
            probeFU = getProbeFU_3_1()
            probeFU2 = getProbeFU_3_2()
            probeFU3 = getProbeFU_3_3()
            probeFU4 = getProbeFU_3_4()
            probeFO = getFoilFO_3_1()
            probeFO2 = getFoilFO_3_2()
            probeFO3 = getFoilFO_3_3()
            probeFO4 = getFoilFO_3_4()
            completeWords = readCompleteWords3()

            #letters = correctLetters3()

        elif lin == lines[3]: #probes & foils from word list 4
            probeNU = getProbeNU_4_1()
            probeNU2 = getProbeNU_4_2()
            probeNU3 = getProbeNU_4_3()
            probeNU4 = getProbeNU_4_4()
            probeFU = getProbeFU_4_1()
            probeFU2 = getProbeFU_4_2()
            probeFU3 = getProbeFU_4_3()
            probeFU4 = getProbeFU_4_4()
            probeFO = getFoilFO_4_1()
            probeFO2 = getFoilFO_4_2()
            probeFO3 = getFoilFO_4_3()
            probeFO4 = getFoilFO_4_4()
            completeWords = readCompleteWords4()

            #letters = correctLetters4()
    
        probes, probes_shuffled = randCat(probeNU, probeFU)
        foils = probeFO

        stimuli = learningPhase(window, ISI, stimuli, setsize, display_duration, completeWords)
        
        test1Mess(window, display_duration) #TEST 1


        for i in range(len(probes_shuffled)):
            start = 0
            probe_i = probes_shuffled[i]
            counter = probeTrial(window, ISI, display_duration, probe_i)
            cell = cell + 1
            start, sheet1 = Scoring(counter, cell, start, sheet1, book)
            sheet1 = probeType(book, sheet1, cell, probe_i, probeNU, probeFU) #add probe type to scoring sheet
            sheet1 = listNum(lin, lines, sheet1, cell, book)#Add list number to scoring sheet
            if (i+1) < 5:
                nextQuestion(window, display_duration)
            else:
                print('End of Test 1')

        #foil question
        start = 0
        counter = foilQuestion(window, ISI, display_duration, probeFO)
        cell = cell + 1
        start, sheet1 = Scoring(counter, cell, start, sheet1, book)
        sheet1 = foilType(book, sheet1, cell, probeFO) #add foil type
        sheet1 = listNum(lin, lines, sheet1, cell, book) #add list number


        #review the list before second test
        listReview(window, ISI, stimuli, setsize, display_duration, completeWords)

        
        test2Mess(window, display_duration) #TEST 2

        probes, probes_shuffled = randCat(probeNU2, probeFU2)
        
        for i in range(len(probes_shuffled)):
            start = 0
            probe_i = probes_shuffled[i]
            counter = probeTrial(window, ISI, display_duration, probe_i)
            cell = cell + 1
            start, sheet1 = Scoring(counter, cell, start, sheet1, book)
            sheet1 = probeType(book, sheet1, cell, probe_i, probeNU2, probeFU2) #add probe type to scoring sheet
            sheet1 = listNum(lin, lines, sheet1, cell, book) #Add list number to scoring sheet
            if (i+1) < 5:
                nextQuestion(window, display_duration)
            else:
                print('End of Test 2')
                
        #for i in range(foils):
        #foil_i = foils[i]
        start = 0
        counter = foilQuestion(window, ISI, display_duration, probeFO2)
        cell = cell + 1
        start, sheet1 = Scoring(counter, cell, start, sheet1, book)
        sheet1 = foilType(book, sheet1, cell, probeFO2) #add foil type
        sheet1 = listNum(lin, lines, sheet1, cell, book) #add list number

        #review the list before second test
        listReview(window, ISI, stimuli, setsize, display_duration, completeWords)

        
        test3Mess(window, display_duration) #TEST 3

        probes, probes_shuffled = randCat(probeNU3, probeFU3)
        
        for i in range(len(probes_shuffled)):
            start = 0
            probe_i = probes_shuffled[i]
            counter = probeTrial(window, ISI, display_duration, probe_i)
            cell = cell + 1
            start, sheet1 = Scoring(counter, cell, start, sheet1, book)
            sheet1 = probeType(book, sheet1, cell, probe_i, probeNU3, probeFU3) #add probe type to scoring sheet
            sheet1 = listNum(lin, lines, sheet1, cell, book) #Add list number to scoring sheet
            if (i+1) < 5:
                nextQuestion(window, display_duration)
            else:
                print('End of Test 3')

        #for i in range(foils):
        #foil_i = foils[i]
        start = 0
        counter = foilQuestion(window, ISI, display_duration, probeFO3)
        cell = cell + 1
        start, sheet1 = Scoring(counter, cell, start, sheet1, book)
        sheet1 = foilType(book, sheet1, cell, probeFO3) #add foil type
        sheet1 = listNum(lin, lines, sheet1, cell, book) #add list number

        #review the list before second test
        listReview(window, ISI, stimuli, setsize, display_duration, completeWords)

        
        test4Mess(window, display_duration) #TEST 4

        probes, probes_shuffled = randCat(probeNU4, probeFU4)
        
        for i in range(len(probes_shuffled)):
            start = 0
            probe_i = probes_shuffled[i]
            counter = probeTrial(window, ISI, display_duration, probe_i)
            cell = cell + 1
            start, sheet1 = Scoring(counter, cell, start, sheet1, book)
            sheet1 = probeType(book, sheet1, cell, probe_i, probeNU4, probeFU4) #add probe type to scoring sheet
            sheet1 = listNum(lin, lines, sheet1, cell, book) #Add list number to scoring sheet
            if (i+1) < 5:
                nextQuestion(window, display_duration)
            else:
                print('End of Test 4')

        #for i in range(foils):
        #foil_i = foils[i]
        start = 0 
        counter = foilQuestion(window, ISI, display_duration, probeFO4)
        cell = cell + 1
        start, sheet1 = Scoring(counter, cell, start, sheet1, book)
        sheet1 = foilType(book, sheet1, cell, probeFO4) #add foil type
        sheet1 = listNum(lin, lines, sheet1, cell, book) #add list number

    now2 = timeNow()
    sheet1 = Goodbye(window, display_duration, now2, sheet1, book)
    book.save(filename)

    window.close()
    


#Functions related to word lists and probe lists -----------------------------------------------------------------------------------------------------------------------------

#READ WORD LISTS 1 THRU 4
    
##
##def correctLetters1():
##    correctletters = open('list1_correct_letters.txt','r')
##    letters = correctletters.readlines()
##    correctletters.close
##    return letters
##
##def correctLetters2():
##    correctletters = open('list2_correct_letters.txt','r')
##    letters = correctletters.readlines()
##    correctletters.close
##    return letters
##
##def correctLetters3():
##    correctletters = open('list3_correct_letters.txt','r')
##    letters = correctletters.readlines()
##    correctletters.close
##    return letters
##
##def correctLetters4():
##    correctletters = open('list4_correct_letters.txt','r')
##    letters = correctletters.readlines()
##    correctletters.close
##    return letters

def readWordList1():
    wordlist = open('word_list_1.txt','r')
    lines = wordlist.readlines()
    wordlist.close
    return lines

def readWordList2():
    wordlist = open('word_list_2.txt','r')
    lines = wordlist.readlines()
    wordlist.close
    return lines

def readWordList3():
    wordlist = open('word_list_3.txt','r')
    lines = wordlist.readlines()
    wordlist.close
    return lines

def readWordList4():
    wordlist = open('word_list_4.txt','r')
    lines = wordlist.readlines()
    wordlist.close
    return lines

def readCompleteWords1():
    wordlist = open('completeWords_1.txt','r')
    completeWords = wordlist.readlines()
    wordlist.close
    return completeWords

def readCompleteWords2():
    wordlist = open('completeWords_2.txt','r')
    completeWords = wordlist.readlines()
    wordlist.close
    return completeWords

def readCompleteWords3():
    wordlist = open('completeWords_3.txt','r')
    completeWords = wordlist.readlines()
    wordlist.close
    return completeWords

def readCompleteWords4():
    wordlist = open('completeWords_4.txt','r')
    completeWords = wordlist.readlines()
    wordlist.close
    return completeWords

def listRandomizer(lines1, lines2, lines3, lines4):
    lines = []
    lines.append(lines1)
    lines.append(lines2)
    lines.append(lines3)
    lines.append(lines4)
    lines_shuffled = lines[:]
    shuffle(lines_shuffled)
    return lines, lines_shuffled
    
    


#--------------------------------------------------------------------------------------------------------------------

#GET PROBE LISTS

#-------------------------------------------------------------------------------------------------------------------------------------------------


#NEAR & UNRELATED (NU)



def getProbeNU_1_1():
    probelist = open('probe_list_NU_1_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_1_2():
    probelist = open('probe_list_NU_1_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_1_3():
    probelist = open('probe_list_NU_1_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_1_4():
    probelist = open('probe_list_NU_1_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_2_1():
    probelist = open('probe_list_NU_2_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_2_2():
    probelist = open('probe_list_NU_2_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_2_3():
    probelist = open('probe_list_NU_2_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_2_4():
    probelist = open('probe_list_NU_2_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_3_1():
    probelist = open('probe_list_NU_3_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU


def getProbeNU_3_2():
    probelist = open('probe_list_NU_3_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU


def getProbeNU_3_3():
    probelist = open('probe_list_NU_3_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU


def getProbeNU_3_4():
    probelist = open('probe_list_NU_3_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU


def getProbeNU_4_1():
    probelist = open('probe_list_NU_4_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU


def getProbeNU_4_2():
    probelist = open('probe_list_NU_4_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

def getProbeNU_4_3():
    probelist = open('probe_list_NU_4_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU


def getProbeNU_4_4():
    probelist = open('probe_list_NU_4_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeNU = []
    for line in p_lines:
        line = line.strip()
        probeNU.append(line)
    return probeNU

#-----------------------------------------------------------------------------------------------------------------------------------------------------

#FAR & UNRELATED (FU)

def getProbeFU_1_1():
    probelist = open('probe_list_FU_1_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_1_2():
    probelist = open('probe_list_FU_1_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_1_3():
    probelist = open('probe_list_FU_1_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_1_4():
    probelist = open('probe_list_FU_1_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_2_1():
    probelist = open('probe_list_FU_2_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_2_2():
    probelist = open('probe_list_FU_2_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_2_3():
    probelist = open('probe_list_FU_2_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_2_4():
    probelist = open('probe_list_FU_2_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_3_1():
    probelist = open('probe_list_FU_3_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_3_2():
    probelist = open('probe_list_FU_3_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_3_3():
    probelist = open('probe_list_FU_3_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_3_4():
    probelist = open('probe_list_FU_3_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

def getProbeFU_4_1():
    probelist = open('probe_list_FU_4_1.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_4_2():
    probelist = open('probe_list_FU_4_2.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_4_3():
    probelist = open('probe_list_FU_4_3.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU


def getProbeFU_4_4():
    probelist = open('probe_list_FU_4_4.txt','r')
    p_lines = probelist.readlines()
    probelist.close
    probeFU = []
    for line in p_lines:
        line = line.strip()
        probeFU.append(line)
    return probeFU

#--------------------------------------------------------------------------------------------------------------------------------------------------------

#OTHER

##def assignCategory(stimuli): #assign probe category
##    
##    NU = [] #near & unrelated
##    NU.append(stimuli[6])
##    NU.append(stimuli[10])
##    NU.append(stimuli[14])
##
##    FU = [] #far & unrelated
##    FU.append(stimuli[4])
##    FU.append(stimuli[20])
##    FU.append(stimuli[36])
##    
##    return NU, FU

def randCat(probeNU, probeFU): #randomize probe category
    probes = []
    probes.append(probeNU)
    probes.append(probeFU)
    #probes.append(probeFO)
    probes_shuffled = probes[:]
    shuffle(probes_shuffled)
    return probes, probes_shuffled


#FUNCTIONS FOR FOILS!

##
##def getUnused(listt): #get unused words from the word list
##    unused = []
##    unused.append(listt[9])
##    unused.append(listt[12])
##    unused.append(listt[21])
##    unused.append(listt[24])
##    return unused
##

def getFoilFO_1_1():
    foillist = open('foils_FO_1_1.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_1_2():
    foillist = open('foils_FO_1_2.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_1_3():
    foillist = open('foils_FO_1_3.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_1_4():
    foillist = open('foils_FO_1_4.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_2_1():
    foillist = open('foils_FO_2_1.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_2_2():
    foillist = open('foils_FO_2_2.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_2_3():
    foillist = open('foils_FO_2_3.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_2_4():
    foillist = open('foils_FO_2_4.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

def getFoilFO_3_1():
    foillist = open('foils_FO_3_1.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_3_2():
    foillist = open('foils_FO_3_2.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_3_3():
    foillist = open('foils_FO_3_3.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_3_4():
    foillist = open('foils_FO_3_4.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_4_1():
    foillist = open('foils_FO_4_1.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_4_2():
    foillist = open('foils_FO_4_2.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_4_3():
    foillist = open('foils_FO_4_3.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO


def getFoilFO_4_4():
    foillist = open('foils_FO_4_4.txt','r')
    f_lines = foillist.readlines()
    foillist.close
    probeFO = []
    for line in f_lines:
        line = line.strip()
        probeFO.append(line)
    return probeFO

main()
